package com.tj.jinzao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseListAdapter;
import com.tj.jinzao.model.Address;

import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 商品 Adapter
 * Created by wjy on 2019/1/10.
 */
public class AddressListItemAdapter extends BaseListAdapter<Address, AddressListItemAdapter.ViewHolder> {

    public AddressListItemAdapter(Context context, CopyOnWriteArrayList data) {
        super(context, data);
    }

    private View.OnClickListener mListener;

    public void setListener(View.OnClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_address_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Address address = mData.get(position);

        holder.name.setText(address.getName());
        holder.phone.setText(address.getMobile());
        holder.address.setText(address.getProvince() + address.getArea());
        holder.edit.setTag(R.id.position, position);
        holder.defaultLabel.setVisibility(address.isIsDefault() ? View.VISIBLE : View.GONE);
        holder.setDefault.setTag(R.id.position, position);
        holder.delete.setTag(R.id.position, position);
        holder.itemLayout.setTag(R.id.position, position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, phone, defaultLabel, address;
        ImageView edit;
        Button setDefault, delete;
        View itemLayout;


        public ViewHolder(View itemView) {
            super(itemView);
            itemLayout = itemView.findViewById(R.id.rl_address_layout);
            name = itemView.findViewById(R.id.tv_name);
            defaultLabel = itemView.findViewById(R.id.tv_default);
            phone = itemView.findViewById(R.id.tv_phone);
            address = itemView.findViewById(R.id.tv_address);
            edit = itemView.findViewById(R.id.iv_edit);
            edit.setOnClickListener(mListener);
            setDefault = itemView.findViewById(R.id.btn_default);
            delete = itemView.findViewById(R.id.btn_delete);
            setDefault.setOnClickListener(mListener);
            delete.setOnClickListener(mListener);
            itemLayout.setOnClickListener(mListener);
        }
    }


}
